package pw.hais.main.main_app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.squareup.okhttp.Response;

import pw.hais.http.base.OnHttpListener;
import pw.hais.main.R;
import pw.hais.main.main_app.app.BaseActivity;
import pw.hais.main.main_app.dao.LeanDao;
import pw.hais.main.main_app.model.LeadModel;
import pw.hais.main.main_app.model.UserModel;
import pw.hais.utils.DownTime;
import pw.hais.utils.EmptyUtil;
import pw.hais.utils.L;

/**
 * 注册页面
 * Created by hais1992 on 2015/12/13.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private EditText edit_mobile,edit_verified_code,edit_username,edit_password;
    private Button btn_get_code,btn_reg;
    private UserModel user;
    private DownTime downTime;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        user = new UserModel();
        initLayoutView();
    }

    private void initLayoutView() {
        edit_mobile = (EditText) findViewById(R.id.edit_mobile);
        edit_verified_code = (EditText) findViewById(R.id.edit_verified_code);
        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_password = (EditText) findViewById(R.id.edit_password);
        btn_get_code = (Button) findViewById(R.id.btn_get_code);
        btn_reg = (Button) findViewById(R.id.btn_reg);
        btn_get_code.setOnClickListener(this);
        btn_reg.setOnClickListener(this);

        downTime = new DownTime(60*10000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                btn_get_code.setText("获取验证码("+((int)millisUntilFinished/1000)+")");
            }

            @Override
            public void onFinish() {
                btn_get_code.setEnabled(true);
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_get_code:
                user.mobilePhone = edit_mobile.getText() == null ? "" : edit_mobile.getText().toString().trim();
                if(!"".equals(user.mobilePhone)){
                    loadDialogShow("发送中....");
                    LeanDao.getMsmCode(user.mobilePhone, new OnHttpListener<LeadModel>() {
                        @Override
                        public void onSuccess(Response response, LeadModel data) {
                            loadDialogDismiss();
                            if(!EmptyUtil.emptyOfString(data.error)){
                                L.showShort(data.error+"");
                            }else {
                                L.showShort("短信已下发！");
                                downTime.start();
                                btn_get_code.setEnabled(false);
                            }
                        }
                    });
                }else{
                    L.showShort("请输入正确的手机号码");
                }
                break;
            case R.id.btn_reg:
                verificationInfoAndPost();
                break;
        }
    }

    //验证信息
    private void verificationInfoAndPost() {
        user.username = edit_username.getText() == null ? "" : edit_username.getText().toString().trim();
        user.password = edit_password.getText() == null ? "" : edit_password.getText().toString().trim();
        user.mobilePhone = edit_mobile.getText() == null ? "" : edit_mobile.getText().toString().trim();
        user.smsCode = edit_verified_code.getText() == null ? "" : edit_verified_code.getText().toString().trim();
        if ("".equals(user.username) || "".equals(user.password) || "".equals(user.mobilePhone)|| "".equals(user.smsCode)) {
            L.showShort("不能为空！");
        } else {
            doRegUser();
        }
    }

    //开始注册
    private void doRegUser() {
        loadDialogShow("注册中....");
        LeanDao.doRegUser(user, new OnHttpListener<UserModel>() {
            @Override
            public void onSuccess(Response response, UserModel data) {
                loadDialogDismiss();
                if(!EmptyUtil.emptyOfString(data.error)){
                    L.showShort(data.error+"");
                }else {
                    L.showShort("注册成功,现在可以进行登录！");
                    finish();
                }
            }
        });
    }


    @Override
    public void finish() {
        super.finish();
        if(downTime!=null)downTime.cancel();
    }
}
